'''
Company: TWL
Author: xue jian
Email: xuejian@kanzhun.com
Date: 2020-11-27 09:33:03
'''
#
# @lc app=leetcode.cn id=454 lang=python3
#
# [454] 四数相加 II
#

# @lc code=start
from typing import List
class Solution:
    def fourSumCount(self, A: List[int], B: List[int], C: List[int], D: List[int]) -> int:
        import collections
        ans=0
        store1=collections.defaultdict(list)
        for i,m in enumerate(A):
            for j, n in enumerate(B):
                store1[m+n].append([i, j])
        for i,m in enumerate(C):
            for j, n in enumerate(D):
                if -(m+n) in store1:
                    ans += len(store1[-(m+n)])
        return ans
# @lc code=end

